﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data;

namespace POSSystem.BLL
{
    public class Products:_Products
    {
        public static Products GetAllActiveProducts()
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static Products GetAllActiveProducts(int branchID)
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = branchID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static Products GetProductByStockKey(int branchID, int stockKey)
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = branchID;
                p.Where.StockKey.Value = stockKey;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static void UpdateCost(int productID, decimal cost)
        {
            try
            {
                Products p = new Products();
                p.LoadByPrimaryKey(productID);
                if(p.RowCount > 0)
                {
                    p.Cost = cost;
                    p.Save();
                }
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "UpdateCost", e.StackTrace);
                throw e;
            }
        }

        public static Products GetProductByBarCode(string barCode)
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.BarCode.Value = barCode;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static string GetProductBarCode(int id)
        {
            try
            {
                Products p = new Products();
                p.Where.ID.Value = id;
                p.Query.AddResultColumn(Products.ColumnNames.BarCode);
                p.Query.Load();

                return p.s_BarCode;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static Products GetNonIMEIActiveProducts()
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.IMEIAvailable.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetNonIMEIActiveProducts", e.StackTrace);
                throw e;
            }
        }

        public static Products GetIMEIActiveProductByBrandID(int brandID)
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.IMEIAvailable.Value = true;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                if(brandID > 0)
                    p.Where.BrandID.Value = brandID;
                p.Query.Load();

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetNonIMEIActiveProducts", e.StackTrace);
                throw e;
            }
        }


        public static Products GetAllProductByCategoriesBrand(String categoryIds, int brandId)
        {
            try
            {
                Products p = new Products();
                if (categoryIds != "")
                {
                    if (categoryIds.Contains('/'))
                        categoryIds = categoryIds.Replace("/", ",");
                    p.Where.CategoryID.Value = categoryIds;
                    p.Where.CategoryID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                }
                if (brandId > 0)
                {
                    p.Where.BrandID.Value = brandId;
                }
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();
                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllProductByCategoriesBrand", e.StackTrace);
                throw e;
            }
        }

        public static Products GetAllProductByCategories(String categoryIds)
        {
            try
            {
                Products p = new Products();

                p.Where.CategoryID.Value = categoryIds;
                p.Where.CategoryID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();
                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllProductByCategoriesBrand", e.StackTrace);
                throw e;
            }
        }

        public static Products GetNonIMEIProductByCategoriesBrand(String categoryIds, int brandId)
        {
            try
            {
                Products p = new Products();
                if (categoryIds != "")
                {
                    if (categoryIds.Contains('/'))
                        categoryIds = categoryIds.Replace("/", ",");
                    p.Where.CategoryID.Value = categoryIds;
                    p.Where.IMEIAvailable.Value = false;
                    p.Where.CategoryID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                }
                if (brandId > 0)
                {
                    p.Where.BrandID.Value = brandId;
                }
                p.Where.Deleted.Value = false;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Load();
                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetAllProductByCategoriesBrand", e.StackTrace);
                throw e;
            }
        }

        public static void SyncProductQty(int productID)
        {
            try
            {
                ListDictionary parameters = new ListDictionary();

                parameters.Add(new SqlParameter("@productID", SqlDbType.Int, 0), productID);

                Products p = new Products();
                p.LoadFromSqlNoExec("SyncProductQty", parameters);
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "SyncProductQty", e.StackTrace);
                throw e;
            }
        }

        public static string UpdateProductQty(int productID, int amt)
        {
            try
            {
                ListDictionary parameters = new ListDictionary();

                parameters.Add(new SqlParameter("@productID", SqlDbType.Int, 0), productID);
                parameters.Add(new SqlParameter("@amt", SqlDbType.Int, 0), amt);
                SqlParameter msg = new SqlParameter("@MSG", SqlDbType.VarChar, 50);
                msg.Direction = ParameterDirection.Output;
                parameters.Add(msg, "");
                
                Products p = new Products();
                p.LoadFromSqlNoExec("UpdateProductQty", parameters);

                return msg.Value.ToString();
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "UpdateProductQty", e.StackTrace);
                throw e;
            }
        }

        public static string UpdateProductSoldQty(int productID, int amt)
        {
            try
            {
                ListDictionary parameters = new ListDictionary();

                parameters.Add(new SqlParameter("@productID", SqlDbType.Int, 0), productID);
                parameters.Add(new SqlParameter("@amt", SqlDbType.Int, 0), amt);
                SqlParameter msg = new SqlParameter("@MSG", SqlDbType.VarChar, 50);
                msg.Direction = ParameterDirection.Output;
                parameters.Add(msg, "");

                Products p = new Products();
                p.LoadFromSqlNoExec("UpdateProductSoldQty", parameters);

                return msg.Value.ToString();
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "UpdateProductQty", e.StackTrace);
                throw e;
            }
        }

        public static Products GetProductsByIds(String ids)
        {
            try
            {
                Products p = new Products();
                if (ids != "")
                {
                    ids = ids.Trim();
                    if (ids.EndsWith(","))
                        ids = ids.Remove(ids.Length - 1);
                    p.Where.ID.Value = ids;
                    p.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                    p.Query.Load();
                }

                return p;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetProductsByIds", e.StackTrace);
                throw e;
            }
        }

        public static string GetProductCategoriesByIds(String ids)
        {
            try
            {
                Products p = new Products();
                if (ids != "")
                {
                    ids = ids.Trim();
                    if (ids.EndsWith(","))
                        ids = ids.Remove(ids.Length - 1);
                    p.Where.ID.Value = ids;
                    p.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                    p.Query.AddResultColumn(Products.ColumnNames.CategoryID);
                    p.Query.Load();

                    string ret = "";
                    for (int i = 0; i < p.RowCount; i++)
                    {
                        ret += p.s_CategoryID + ",";
                        p.MoveNext();
                    }
                    if (ret != "" && ret.EndsWith(","))
                        ret = ret.Remove(ret.Length - 1);
                    return ret;
                }

                return "";
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetProductsByIds", e.StackTrace);
                throw e;
            }
        }

        public static string GetProductNameById(int id)
        {
            try
            {
                Products p = new Products();
                p.Query.AddResultColumn(Products.ColumnNames.ProductName);
                p.Where.ID.Value = id;
                p.Query.Load();

                return p.RowCount > 0 ? p.ProductName : "";
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetProductsByIds", e.StackTrace);
                throw e;
            }
        }
    }
}
